package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.knowledge.api.domain.RemotePersonalTreeService;
import cn.cdeden.knowledge.api.domain.vo.RemotePersonalTreeVo;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.service.PersonalFilesTreeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识管理服务
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemotePersonalServiceImpl implements RemotePersonalTreeService {

    private final PersonalFilesTreeService personalFilesTreeService;

    @Override
    public RemotePersonalTreeVo getById(String userId) {
        PersonalFilesTree personalFilesTree = personalFilesTreeService.selectById(userId);
        return MapstructUtils.convert(personalFilesTree, RemotePersonalTreeVo.class);
    }

    @Override
    public Boolean judgeIsEmpty(String userId, String username, String folderName, String parentId, String type, String suffix) {
        return personalFilesTreeService.judgeIsEmpty(userId, username, folderName, parentId, type, suffix);
    }

    @Override
    public Boolean saveBatch(List<RemotePersonalTreeVo> filesTrees) {
        List<PersonalFilesTree> personalFilesTrees = new ArrayList<>();
        filesTrees.forEach( v -> {
            PersonalFilesTree personalFilesTree = new PersonalFilesTree();
            personalFilesTree.setId(v.getId());
            personalFilesTree.setSort(v.getSort());
            personalFilesTree.setTopParentId(v.getTopParentId());
            personalFilesTree.setParentId(v.getParentId());
            personalFilesTree.setSuperiorsIds(v.getSuperiorsIds());
            personalFilesTree.setSuffix(v.getSuffix());
            personalFilesTree.setRealName(v.getRealName());
            personalFilesTree.setSuperiorsPath(v.getSuperiorsPath());
            personalFilesTree.setPath(v.getPath());
            personalFilesTree.setType(v.getType());
            personalFilesTree.setStorageId(v.getStorageId());
            personalFilesTree.setStorageType(v.getStorageType());
            personalFilesTree.setSize(v.getSize());
            personalFilesTree.setEditionName(v.getEditionName());
            personalFilesTree.setEdition(v.getEdition());
            personalFilesTree.setFileMd5(v.getFileMd5());
            personalFilesTree.setLinkingState(v.getLinkingState());
            personalFilesTree.setCreateId(v.getCreateId());
            personalFilesTree.setCreateBy(v.getCreateBy());
            personalFilesTree.setCreateTime(v.getCreateTime());
            personalFilesTree.setUpdateId(v.getUpdateId());
            personalFilesTree.setUpdateBy(v.getUpdateBy());
            personalFilesTree.setUpdateTime(v.getUpdateTime());
            personalFilesTree.setDeleteId(v.getDeleteId());
            personalFilesTree.setDeleteBy(v.getDeleteBy());
            personalFilesTree.setDeleteTime(v.getDeleteTime());
            personalFilesTree.setDeleteType(v.getDeleteType());
            personalFilesTree.setRemarks(v.getRemarks());
            personalFilesTree.setSys(v.getSys());
            personalFilesTree.setDocContent(v.getDocContent());
            personalFilesTree.setCollection(v.getCollection());
            personalFilesTrees.add(personalFilesTree);
        });

        return personalFilesTreeService.saveBatch(personalFilesTrees);
    }

    @Override
    public boolean updateEs(String id, String esID) {
//        return personalFilesTreeService.updateEs(id, esID);
        return false;
    }

    @Override
    public List<RemotePersonalTreeVo> getFileNameSuffix(List<String> fileIds) {
        List<PersonalFilesTree> personalFilesTrees = personalFilesTreeService.list(new LambdaQueryWrapper<PersonalFilesTree>().in(PersonalFilesTree::getId, fileIds));
        return MapstructUtils.convert(personalFilesTrees, RemotePersonalTreeVo.class);
    }
}
